<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>算法</title>
</head>
<body>
  
<script type="text/javascript">
  // 冒泡排序
  function fn1(arr) {
    let temp = ''
    for(let i = 0; i < arr.length - 1; i++) {
      for(let j = i + 1; j < arr.length; j++) {
        if (arr[i] > arr[j]) {
          temp = arr[i]
          arr[i] = arr[j]
          arr[j] = temp
        }
      }
    }
    return arr
  }
  console.log(fn1([1, 3, 44, 12, 4, 55, 6]))

  // 回文
  function fn2(str) {
    return str.split('').reverse().join('')
  }
  console.log(fn2('abcdef'))

  // 数组去重
  function fn3(arr) {
    let obj = {}
    let resArr = []
    for(let i = 0; i < arr.length; i++) {
      if(!obj[arr[i]]) {
        obj[arr[i]] = true
        resArr.push(arr[i])
      }
    }
    return resArr
  }
  console.log(fn3([11, 3, 11, 12, 4, 3, 12]))

  // 返回字符串中出现次数最多的数字
  function fn4(str) {
    let obj = {}
    for(let i = 0; i < str.length; i++) {
      if (obj[str[i]]) {
        obj[str[i]] += 1
      } else {
        obj[str[i]] = 1
      }
    }
    let max = 0;
    let key = ''
    for(var k in obj) {
      if (obj[k] > max) {
        max = obj[k]
        key = k
      }
    }
    return key + ':' + max
  }
  console.log(fn4('aaaabbdddfffcccttttttttt'))

</script>
</body>
</html>